package com.windowes.weichang.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dao.DemandArrivalMapper;
import com.windowes.weichang.dao.DemandCancleLogMapper;
import com.windowes.weichang.dao.DemandMapper;
import com.windowes.weichang.dao.WShopMapper;
import com.windowes.weichang.pojo.Demand;
import com.windowes.weichang.pojo.WShop;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/7/17 下午9:15
 * @description:
 *
 **/
@Service
public class FormResportService {

    @Autowired
    WShopMapper shopMapper;
    @Autowired
    DemandMapper demandMapper;
    @Autowired
    DemandArrivalMapper arrivalMapper;
    @Autowired
    DemandCancleLogMapper demandCancleLogMapper;


    public ServerResponse order(String startTime, String endTime, int pageNo, int pageSize) {

        //存在为空的时间,直接清空两个时间
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            startTime = null;
            endTime = null;
        }

        Page<WShop> page = new Page<>(pageNo, pageSize);
        page.setRecords(shopMapper.orderList(startTime, endTime));

        for (WShop shop : page.getRecords()) {
            List<Integer> shopIds = new ArrayList<>();
            shopIds.add(shop.getId());
            List<Demand> demands = demandMapper.selectByShopIds(shopIds);
            //开始计算订单量
            int size = demands.size();
            shop.setOrderNum(size);
            BigDecimal totalMoney = demands.stream().map(Demand::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
            //计算总交易额
            shop.setTotalPrice(totalMoney);
            //订单均单价
            shop.setAverageOrderPrice(new BigDecimal(0.0));
            if (size > 0) {
                shop.setAverageOrderPrice(shop.getTotalPrice().divide(new BigDecimal(size), RoundingMode.HALF_UP));
            }
        }
        return ServerResponse.createBySuccess(page);
    }

    public ServerResponse service(String startTime, String endTime, int pageNo, int pageSize) {
        //存在为空的时间,直接清空两个时间
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            startTime = null;
            endTime = null;
        }

        Page<WShop> page = new Page<>(pageNo, pageSize);
        page.setRecords(shopMapper.selectAll());

        for (WShop shop : page.getRecords()) {
            //取消预约次数
            int cancleCount = arrivalMapper.countByStatus(shop.getId(), 2, startTime, endTime);
            //上门次数
            int count = arrivalMapper.countByStatus(shop.getId(), null, startTime, endTime);
            //开始计算出勤率
            int arrivalCount = count - cancleCount;
            shop.setArrivalCount(arrivalCount);
            shop.setParticipateRate(0L);
            if (count > 0) {
                shop.setParticipateRate((long) arrivalCount / count);
            }
            //取消订单次数
            int cancleDemandCount = demandCancleLogMapper.countByShopId(shop.getId(), startTime, endTime);
            shop.setCancleCount(cancleDemandCount);
        }

        return ServerResponse.createBySuccess(page);
    }
}
